package com.rd.student.ruanjian.class4.zhouzihe.practice;

import java.util.concurrent.locks.ReentrantLock;

public class TestBank extends Thread {
    //银行需要有账户来取钱
    //Bank has a Account 构成一个类的组合关系
    static Account account;
    //取钱的金额
    static double money;
    ReentrantLock lock = new ReentrantLock();
    Object object = new Object();

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public void run() {

        //取钱的操作

            if (account.balance < money) {
                System.out.println("余额不足");
                return;
            }

            System.out.println(getName() + "可以取钱");
            account.balance = account.balance - money;
            System.out.println("取钱后的余额为" + account.balance);
    }
    public synchronized void test1(){
        if (account.balance < money) {
            System.out.println("余额不足");
            return;
        }

        System.out.println(getName() + "可以取钱");
        account.balance = account.balance - money;
        System.out.println("取钱后的余额为" + account.balance);
    }


    public TestBank(Account account, double money,String name) {
        super(name);
        this.account = account;
        this.money = money;
    }

    public static void main(String[] args) {
        Account account = new Account("123456",888);
        new TestBank(account, 666, "zhangsan").start();
        new TestBank(account, 666, "lisi").start();

    }
}


//创建一个账户类
class Account {
    //属性设置未账户id和余额
    String accountId;
    double balance;

    public Account(String accountId, double balance) {
        this.accountId = accountId;
        this.balance = balance;
    }
}